import requests
import hashlib
import time

# 注册
s = requests.session()
username = int(time.time())
mydata = {
    'username': username,
    'password': 'admin123',
    'cpassword': 'admin123',
    'checkcode': '1234',
    'email': f'{int(time.time())}@qq.com'
}
res = s.post(url='http://114.132.200.42:3456/user/register', data=mydata)
print(res.json())


# 登录
def make_md5(text):
    m = hashlib.md5()  # 创建一个 md5加密工具
    m.update(str(text).encode(encoding='utf-8'))
    res = m.hexdigest()  # 生成加密后的内容
    return res


s = requests.session()
url = 'http://114.132.200.42:3456/user/login'
data = {
    'username': username,
    'password': make_md5('py1234'),
    'checkcode': '1234'
}
res = s.post(url=url, data=data)
print(res.json())
getjson = res.json()
getuserid = getjson['userId']
getusertoken = getjson['userToken']

# 创建任务
infourl = 'http://114.132.200.42:3456/task/info'
mydata = {'title': '我的新任务'}
hd = {'userId': getuserid, 'userToken': getusertoken}
res = s.post(url=infourl, data=mydata, headers=hd)
print(res.json())
getjson_taskid = res.json()
gettaskid = getjson_taskid['taskid']

# 查询任务
getinfourl = 'http://114.132.200.42:3456/task/info'
mydata = {'status': '1'}
res = s.get(url=getinfourl, params=mydata, headers=hd)
print(res.json())

# 完成任务
getinfo_T = 'http://114.132.200.42:3456/task/info'
mydata = {'task_id': gettaskid}
res = s.put(url=getinfo_T, data=mydata, headers=hd)
print(res.json())

# 取消完成任务
getinfo_F = 'http://114.132.200.42:3456/task/info'
mydata = {'task_id': gettaskid}
res = s.put(url=getinfo_F, data=mydata, headers=hd)
print(res.json())

# 删除任务
getdelete = 'http://114.132.200.42:3456/task/info'
mydata = {'task_id': gettaskid}
res = s.delete(url=getdelete, data=mydata, headers=hd)
print(res.json())

# 上传头像
imgusl = 'http://114.132.200.42:3456/user/uploal_action'
myimg = [
    ('pic', ('1.jpg', open('1.jpg', 'rb')))
]
res = s.post(url=imgusl, headers=hd, files=myimg)
print(res.json())
